"""
模拟数据系统
完全使用模拟数据，无需申请任何API
适合演示和测试
"""
import json
from datetime import datetime, timedelta
import random

class MockDataSystem:
    def __init__(self):
        self.train_data = self.load_train_data()
        self.flight_data = self.load_flight_data()
        self.bus_data = self.load_bus_data()
    
    def load_train_data(self):
        """加载火车数据"""
        return {
            ('北京', '上海'): [
                {'train_number': 'G1', 'departure_time': '08:00', 'arrival_time': '12:30', 'duration': '4.5小时', 'price': 553},
                {'train_number': 'G3', 'departure_time': '10:00', 'arrival_time': '14:30', 'duration': '4.5小时', 'price': 553},
                {'train_number': 'G5', 'departure_time': '14:00', 'arrival_time': '18:30', 'duration': '4.5小时', 'price': 553}
            ],
            ('北京', '广州'): [
                {'train_number': 'G79', 'departure_time': '09:00', 'arrival_time': '18:30', 'duration': '9.5小时', 'price': 862},
                {'train_number': 'G81', 'departure_time': '11:00', 'arrival_time': '20:30', 'duration': '9.5小时', 'price': 862}
            ],
            ('上海', '杭州'): [
                {'train_number': 'G7501', 'departure_time': '07:00', 'arrival_time': '08:30', 'duration': '1.5小时', 'price': 73},
                {'train_number': 'G7503', 'departure_time': '09:00', 'arrival_time': '10:30', 'duration': '1.5小时', 'price': 73}
            ],
            ('上海', '深圳'): [
                {'train_number': 'G99', 'departure_time': '08:00', 'arrival_time': '14:30', 'duration': '6.5小时', 'price': 553}
            ]
        }
    
    def load_flight_data(self):
        """加载飞机数据"""
        return {
            ('北京', '上海'): [
                {'flight_number': 'CA1234', 'departure_time': '09:30', 'arrival_time': '12:00', 'duration': '2.5小时', 'price': 800, 'airline': '中国国航'},
                {'flight_number': 'MU5678', 'departure_time': '14:30', 'arrival_time': '17:00', 'duration': '2.5小时', 'price': 750, 'airline': '东方航空'}
            ],
            ('北京', '广州'): [
                {'flight_number': 'CA1235', 'departure_time': '08:00', 'arrival_time': '11:30', 'duration': '3.5小时', 'price': 1200, 'airline': '中国国航'},
                {'flight_number': 'CZ1236', 'departure_time': '15:00', 'arrival_time': '18:30', 'duration': '3.5小时', 'price': 1100, 'airline': '南方航空'}
            ],
            ('上海', '深圳'): [
                {'flight_number': 'CA1237', 'departure_time': '10:00', 'arrival_time': '12:30', 'duration': '2.5小时', 'price': 900, 'airline': '中国国航'}
            ]
        }
    
    def load_bus_data(self):
        """加载汽车数据"""
        return {
            ('北京', '上海'): [
                {'description': '北京-上海长途汽车', 'departure_time': '08:00', 'arrival_time': '20:00', 'duration': '12小时', 'price': 200}
            ],
            ('北京', '广州'): [
                {'description': '北京-广州长途汽车', 'departure_time': '09:00', 'arrival_time': '次日06:00', 'duration': '21小时', 'price': 350}
            ],
            ('上海', '杭州'): [
                {'description': '上海-杭州长途汽车', 'departure_time': '08:30', 'arrival_time': '11:00', 'duration': '2.5小时', 'price': 80}
            ]
        }
    
    def get_routes(self, origin, destination, transport_type=None):
        """获取路线信息"""
        routes = []
        
        # 获取火车信息
        if not transport_type or transport_type == 'train':
            train_routes = self.train_data.get((origin, destination), [])
            for route in train_routes:
                routes.append({
                    'type': 'high_speed_rail',
                    'train_number': route['train_number'],
                    'departure_time': route['departure_time'],
                    'arrival_time': route['arrival_time'],
                    'duration': route['duration'],
                    'price': route['price'],
                    'origin': origin,
                    'destination': destination
                })
        
        # 获取飞机信息
        if not transport_type or transport_type == 'flight':
            flight_routes = self.flight_data.get((origin, destination), [])
            for route in flight_routes:
                routes.append({
                    'type': 'flight',
                    'flight_number': route['flight_number'],
                    'departure_time': route['departure_time'],
                    'arrival_time': route['arrival_time'],
                    'duration': route['duration'],
                    'price': route['price'],
                    'airline': route['airline'],
                    'origin': origin,
                    'destination': destination
                })
        
        # 获取汽车信息
        if not transport_type or transport_type == 'bus':
            bus_routes = self.bus_data.get((origin, destination), [])
            for route in bus_routes:
                routes.append({
                    'type': 'bus',
                    'description': route['description'],
                    'departure_time': route['departure_time'],
                    'arrival_time': route['arrival_time'],
                    'duration': route['duration'],
                    'price': route['price'],
                    'origin': origin,
                    'destination': destination
                })
        
        return routes
    
    def get_smart_routes(self, origin, destination):
        """获取智能路线推荐"""
        # 检查是否有直达高铁
        direct_trains = self.train_data.get((origin, destination), [])
        
        if direct_trains:
            # 有直达高铁
            response = f"🚄 从{origin}到{destination}有直达高铁！\\n\\n"
            response += self.format_train_info(direct_trains[0])
            response += "\\n\\n💡 其他选择：\\n"
            response += self.format_alternatives(origin, destination)
        else:
            # 没有直达高铁，提供联运方案
            response = f"🔄 从{origin}到{destination}没有直达高铁，为您推荐联运方案：\\n\\n"
            response += self.format_transfer_routes(origin, destination)
            response += "\\n\\n💡 其他选择：\\n"
            response += self.format_alternatives(origin, destination)
        
        return response
    
    def format_train_info(self, train):
        """格式化火车信息"""
        return f"🚄 {train['train_number']}次列车\\n" + \
               f"⏰ {train['departure_time']} - {train['arrival_time']} ({train['duration']})\\n" + \
               f"💰 票价：¥{train['price']}\\n" + \
               f"🎫 建议提前购票"
    
    def format_alternatives(self, origin, destination):
        """格式化备选方案"""
        response = ""
        
        # 飞机信息
        flights = self.flight_data.get((origin, destination), [])
        if flights:
            flight = flights[0]
            response += f"✈️ 飞机：{flight['flight_number']} ({flight['airline']})\\n"
            response += f"    ⏰ {flight['departure_time']} - {flight['arrival_time']} ({flight['duration']})\\n"
            response += f"    💰 票价：¥{flight['price']}\\n\\n"
        else:
            response += f"✈️ 飞机：约2-3小时，¥800-1200\\n\\n"
        
        # 汽车信息
        buses = self.bus_data.get((origin, destination), [])
        if buses:
            bus = buses[0]
            response += f"🚌 长途汽车：{bus['description']}\\n"
            response += f"    ⏰ {bus['departure_time']} - {bus['arrival_time']} ({bus['duration']})\\n"
            response += f"    💰 票价：¥{bus['price']}\\n\\n"
        else:
            response += f"🚌 长途汽车：约8-12小时，¥200-400\\n\\n"
        
        # 自驾信息
        response += f"🚗 自驾：约{self.get_driving_time(origin, destination)}小时"
        
        return response
    
    def format_transfer_routes(self, origin, destination):
        """格式化联运路线"""
        transfer_routes = [
            f"{origin} → 武汉 → {destination} (高铁+高铁)",
            f"{origin} → 郑州 → {destination} (高铁+高铁)",
            f"{origin} → 南京 → {destination} (高铁+汽车)",
            f"{origin} → 合肥 → 武汉 → {destination} (高铁+高铁+高铁)"
        ]
        
        response = ""
        for i, route in enumerate(transfer_routes, 1):
            response += f"方案{i}：{route}\\n"
            response += f"⏱️ 总时间：{6 + i * 0.5}小时\\n"
            response += f"💰 总费用：¥{500 + i * 100}\\n"
            response += f"🔄 换乘：{i}次\\n\\n"
        
        return response
    
    def get_driving_time(self, origin, destination):
        """获取自驾时间"""
        distances = {
            ('北京', '上海'): 12,
            ('北京', '广州'): 20,
            ('上海', '杭州'): 3,
            ('上海', '深圳'): 15
        }
        return distances.get((origin, destination), 8)

# 使用示例
if __name__ == "__main__":
    api = MockDataSystem()
    routes = api.get_smart_routes('北京', '上海')
    print(routes)